<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ChangeStatus as ExtensionsChangeStatus;
use App\Models\OnlineClassroom;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;

class OnlineClassroomController extends AdminController
{

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new OnlineClassroom(), function (Grid $grid) {

            $grid->column('id')->sortable();
            $grid->column('title')->limit(15, '...');
            $grid->column('description')->limit(15, '...');
            $grid->column('cover')->image('', 120, 60);
            $grid->column('url')->link();
            $grid->column('tag')->explode()->label();
            $grid->column('order')->badge()->editable(true)->sortable();
            $grid->column('status')->using([1 => '在线', 0 => '下线'])
                ->label(
                    [
                        0 => 'gray',
                        1 => 'green'
                    ]
                );
            // $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->prepend(new ExtensionsChangeStatus(OnlineClassroom::class));
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new OnlineClassroom(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('description');
            $show->field('cover')->image();
            $show->field('url');
            $show->field('tag')->explode()->label();
            $show->field('order')->badge();
            $show->field('status')->using([1 => '在线', 0 => '下线'])
                ->label('green');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new OnlineClassroom(), function (Form $form) {

            $form->display('id');
            $form->text('title');
            $form->textarea('description');
            $form->image('cover')->saveFullUrl()->autoUpload()
                ->accept('jpg,png,gif,jpeg');
            $form->url('url');

            $form->tags('tag')->help('标签输入完成后要以逗号结尾')
                ->saving( function ($v) {
                    return implode(',', $v);
                });
            $form->number('order')->default(1);
            $form->switch('status')->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
